package com.control.action;

import java.util.Collection;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.model.entity.Student;
import com.model.imp.StudentImp;
import com.model.util.PageController;

public class StudentAction {
	private int[] chkStudent;
	private String stuname;
	private String stunumber;
	public String getStunumber() {
		return stunumber;
	}

	public void setStunumber(String stunumber) {
		this.stunumber = stunumber;
	}

	private Student student;
	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public int[] getChkStudent() {
		return chkStudent;
	}

	public void setChkStudent(int[] chkStudent) {
		this.chkStudent = chkStudent;
	}



	public String getStuname() {
		return stuname;
	}

	public void setStuname(String stuname) {
		this.stuname = stuname;
	}

	public String list() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String cNumber = request.getParameter("currentPageNo");
		String toNumber = request.getParameter("toPageNo");
		int start = 0;
		if (request.getAttribute("currentPageNo") != null) {
			int pageNumber = Integer.parseInt((String) request
					.getAttribute("currentPageNo"));
			if (pageNumber == 1) {
				start = 1;
			}
		}

		if (cNumber != null && toNumber != null) {
			int tn = Integer.parseInt(toNumber);
			// 根据数据库中数据总数，判断所有结果显示的总页数
			// 如果总页数太小，设初始值为 1
			if (tn >= 0) {
				start = tn;
			}
		}

		StudentImp dao = new StudentImp();
		int total = dao.getTotal();
		PageController pc = new PageController(total, start);
		Collection<Student> list = dao.getStudents(pc.getPageStartRow(), pc
				.getPageSize());
		// 2 把 list 对象，封装到 request 作用域对象中
		request.setAttribute("list", list);
		request.setAttribute("pageController", pc); // 控制分页的对象
		return "list";
	}

	public String detail() throws Exception {
		StudentImp dao = new StudentImp();
		Student student = dao.findByName(stuname);
		ServletActionContext.getRequest().setAttribute("student", student);
		return "studentdetail";
	}

	public String delete() throws Exception {
		StudentImp dao = new StudentImp();
		if (chkStudent.length > 0) {
			System.out.print(" chkstudnet");

			for (int id : chkStudent) {
				Student students = dao.findById(id);
				dao.delStudent(students);
				return this.list();
			}
		}
		return "list";
	}
	
	public String add() throws Exception {
		StudentImp dao = new StudentImp();
		if(dao.add(student)){
		   return this.list();
		}else{
			return "error";
		}
		  
		
	}
	
	public String edit() throws Exception {
		StudentImp dao = new StudentImp();
		if (chkStudent.length > 0){
			for (int id : chkStudent) {
			 student = dao.findById(id);
			     return "editstudent";
			}
		}else{
			return "error";
		}
		return "list";
	}

/*	
	public String exist() throws Exception {
		StudentImp dao = new StudentImp();
		if(dao.findByNumber(stunumber)!=null){
		  
		}else{
			
		}
		  
		
	}*/
	public String execute() throws Exception {

		return "succuss";
	}

}
